<template>
  <div class="login-container">
    <el-form ref="myform" class="myfrom" :model="form" :rules="rules">
      <h3>{{ $t("login.title") }}</h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机号"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          placeholder="请输入密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" class="btn" @click="login"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>
<script>
//导入登录方法
// import { loginApi } from '@/api/user.js'
//导入 vuex
import { mapActions } from "vuex";
import { setTimeout } from "timers";
export default {
  data() {
    return {
      //用户信息
      form: {
        mobile: "13800000002",
        password: "123456"
      },
      //校验规则
      rules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            validator: function(rule, value, callback) {
              //定义手机好正则
              var dym = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              //测试正则
              dym.test(value)
                ? callback()
                : callback(new Error("手机号格式不正确"));
            }
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度范围是6~16 个字符",
            trigger: "blur"
          }
        ]
      },
      //按钮的加载效果
      loading: false
    };
  },
  methods: {
    ...mapActions({
      userlogin: "user/login"
    }),
    //提示登录的参数
    login() {
      //验证表单中的数据
      this.$refs.myform.validate(async valid => {
        if (valid) {
          //开启加载效果
          this.loading = true;
          try {
            //验证通过
            await this.userlogin(this.form); //发送网络请求
            console.log("登录成功");
            //接收 _redirec
            const _redirect = this.$route.query._redirect;
            //判断是否存在参数
            if (_redirect) {
              // 跳转到 _redirect 对应的路由
              this.$router.push(_redirect);
            } else {
              //跳转到主页
              this.$router.push("/");
            }

            //登录成功的提示信息
            this.$message.success("登录成功");
            // const res = await loginApi(this.form)
            // console.log(res)
          } catch (error) {
            console.log(error.message);
          } finally {
            //取消loading
            this.loading = false;
          }
        } else {
          //验证失败
          console.log("验证失败");
        }
      });
    }
  }
};
</script>

<style lang="scss">
.login-container {
  background-image: url("../../assets/common/login.jpg");
  height: 100%;
  background-position: center;
  .myfrom {
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: fixed;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      font-size: 43px;
      font-size: 100;
      color: #fff;
      text-align: center;
      letter-spacing: 6px;
    }
    .btn {
      width: 100%;
      height: 60px;
      background: #407ffe;
      border: none;
      color: #fff;
      font-size: 20px;
    }
    input.el-input__inner {
      height: 50px;
    }
    .mark {
      color: #fff;
      text-align: center;
      line-height: 50px;
    }
  }
}
</style>
